﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Helper.Database;
using System.Data;
using System.Data.SqlClient;
using System.Collections.Specialized;
using System.Xml;
namespace TestConsole
{
    class ConfigManager
    {
        public static NameValueCollection GetConfigSettings(string path)
        {
            if (string.IsNullOrEmpty(path)) throw new ApplicationException("config file path is not valid!");
            NameValueCollection nvc = new NameValueCollection();
            XmlReader reader = XmlReader.Create(path);
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.XmlDeclaration:
                        break;
                    case XmlNodeType.Attribute:
                        break;
                    case XmlNodeType.CDATA:
                        break;
                    case XmlNodeType.Element:
                        // Console.WriteLine(reader.LocalName);
                        if (reader.HasAttributes && reader.AttributeCount == 2 && !string.IsNullOrEmpty(reader.GetAttribute("key")))
                        {
                            // Console.WriteLine(reader.GetAttribute("key") + "==" + reader.GetAttribute("value"));
                            nvc.Add(reader.GetAttribute("key"), reader.GetAttribute("value"));
                        }

                        break;
                    case XmlNodeType.Comment:
                        break;
                    case XmlNodeType.Whitespace:
                        break;
                }
            }

            return nvc;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            //NameValueCollection nvc = Helper.ConfigManager.GetConfigSettings("cfg.config");

            //foreach (string key in nvc.AllKeys)
            //{
            //    Console.WriteLine(key + ",," + nvc[key]);
            //}

            //Person source = new Person() { ID = 1, Name = "niejunhua1" }, target = new Person() { ID = 2, Name = "jing" };

            //Helper.Database.DataMapper.Clone<Person>(source, target);
            //Console.WriteLine(target.ID + ".." + target.Name);

            TestDataBase tdb = new TestDataBase();
            //  tdb.DataTable1();
            //  tdb.GetEntity();
            tdb.GetEntityList();

            ////TestMysql mydb = new TestMysql();
            ////mydb.GetList();

            //TestOledb oldb = new TestOledb();
            //oldb.DataTable1();
            //oldb.GetList();





        }
    }

    class TestOledb
    {
        NjhData nda = new Helper.Database.NjhData(Helper.Database.DataType.Oledb, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\bak\\company.mdb;");
        public void DataTable1()
        {
            DataTable dt = null;
            for (int i = 0; i < 1; i++)
            {
                string sql = " select top 1111 * from companyinfo  ";
                dt = nda.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    // Console.WriteLine(dr[4]);
                }
            }
            MyWatch sp = new MyWatch();
            sp.Start();
            List<Company> list = nda.GetEntityList<Company>(dt);
            sp.End();
        }
        public void GetEntity()
        {
            Company channel = nda.GetEntity<Company>(" select top 1 * from companyinfo where id=354");
            if (channel == null) Console.WriteLine("Channel s null");
            else Console.WriteLine(channel.CompanyName);
        }

        public void GetList()
        {
            int iii = 304;
            MyWatch sp = new MyWatch();
            sp.Start();
            for (int i = 1; i <= 1; i++)
            {
                // iii += 50;
                List<Company> list = nda.GetEntityList<Company>("select top 1111 * from companyinfo where id>" + iii);
                foreach (Company c in list)
                {
                    //  Console.WriteLine(c.CompanyName + "..." + c.Contact + "..." + c.ID);
                }
                //  GC.Collect();
                //  System.Threading.Thread.Sleep(1500);
            }
            sp.End();
        }

    }

    class TestMysql
    {
        NjhData nda = new Helper.Database.NjhData(Helper.Database.DataType.Mysql, "server=127.0.0.1;uid=root;password=;database=nsys");
        public void DataTable1()
        {
            for (int i = 0; i < 1; i++)
            {
                string sql = " select * from channel  ";
                DataTable dt = nda.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    Console.WriteLine(dr[1]);
                }
            }
        }

        public void GetEntity()
        {
            Channel channel = nda.GetEntity<Channel>("select * from channel limit 1,1");
            if (channel == null) Console.WriteLine("Channel s null");
            else Console.WriteLine(channel.Title);
        }

        public void GetList()
        {
            for (int i = 1; i <= 100; i++)
            {
                List<Channel> list = nda.GetEntityList<Channel>("select * from channel");
                foreach (Channel c in list)
                {
                    Console.WriteLine(c.Title + "..." + c.Url + "..." + c.OrderIndex);
                }
            }
        }
    }

    class TestDataBase
    {
        // string server = "server=192.168.104.117;uid=wftsa;password=jd7nTF#wM;database=K2Sln";
        NjhData nda = new Helper.Database.NjhData(Helper.Database.DataType.Mssql, "server=.;uid=sa;password=niejunhua;database=AdventureWorks2008");

        public void DataTable1()
        {
            for (int i = 0; i < 1; i++)
            {
                string sql = " select top 888 * from Person.Address";
                DataTable dt = nda.ExecuteDataTable(sql);
                foreach (DataRow dr in dt.Rows)
                {
                    //  Console.WriteLine(dr[5]);
                }
                MyWatch sp = new MyWatch();
                sp.Start();
                List<Address> list = nda.GetEntityList<Address>(dt);
                foreach (Address vr in list)
                {
                    Console.WriteLine(vr.rowguid);
                }
                sp.End();

            }
        }

        public void GetEntity()
        {
            List<double> list = new List<double>();
            for (int yi = 0; yi <= 100; yi++)
            {
                for (int i = 1; i <= 1000; i++)
                {
                    //  NjhData db = new Helper.Database.NjhData(Helper.Database.DataType.Mssql, "server=192.168.10.37;uid=sa;password=niejunhua;database=NT");
                    MyWatch sp = new MyWatch();
                    sp.Start();
                    Address entity = nda.GetEntity<Address>("select top 1 * from person.[Address] ");
                    // Console.WriteLine(entity.rowguid);

                    //IDataReader dr = nda.ExecuteReader("select top 1 * from person.[Address] ");
                    //if (dr.Read())
                    //{
                    //    Address aaa = new Address
                    //    {
                    //        rowguid = Guid.Parse(dr["rowguid"].ToString())
                    //    };
                    //    Console.WriteLine(aaa.rowguid);
                    //}

                    list.Add(sp.End());
                }

                list = list.OrderByDescending(x => x).ToList();
                foreach (double i in list)
                {
                    //  Console.WriteLine(i);
                }

                Console.WriteLine("avg:" + list.Average());
            }
        }

        public void GetEntityList()
        {
            List<double> list = new List<double>();
            for (int j = 1; j <= 2; j++)
            {
                for (int i = 1; i <= 1; i++)
                {
                    //    NjhData db = new Helper.Database.NjhData(Helper.Database.DataType.Mssql, "server=192.168.10.37;uid=sa;password=niejunhua;database=NT");
                    MyWatch sp = new MyWatch();
                    sp.Start();
                    var entity = nda.GetEntityList<Address>("select top 111 * from  person.[Address] ");

                    foreach (var a in entity)
                    {
                        //  Console.WriteLine(a.AddressLine1);
                    }
                    //Console.WriteLine(entity.Count);
                    var rs = sp.End();
                    // if (i != 1)
                    list.Add(rs);
                    Console.WriteLine(rs);
                }
                //var entity = db.ExecuteScalar<int>("select idx from test where idx=199999900", -1);
            }
            Console.WriteLine(list.Average() + "abf");

            //nda.Insert<Address>(new Address
            //{
            //    //AddressID = 991999,
            //    AddressLine1 = "line1" + DateTime.Now,
            //    AddressLine2 = "lien2",
            //    rowguid = Guid.NewGuid(),
            //    City = "shanghai",
            //    ModifiedDate = DateTime.Now,
            //    PostalCode = "aaaaaa",
            //    StateProvinceID = 1

            //});
        }
    }

    class MyWatch
    {
        private int q1 = 0;
        System.Diagnostics.Stopwatch sp = new System.Diagnostics.Stopwatch();
        public void Start()
        {
            sp.Start();
        }

        public double End()
        {
            sp.Stop();
            double s = sp.Elapsed.TotalMilliseconds / 1000;
            //  Console.WriteLine(s);
            return s;
        }

    }
}
